package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Observable;

import javax.swing.*;

import conference.Conference;

/**
 * A GUI class that asks the user to select a conference.
 * @author Allison Graham
 * @version 1.0
 */

public class ChooseConferencePanel extends Observable {
	private static final String TITLE = "Choose a Conference";
	public static int BORDER_SPACE = 20; /** Distance between components.*/
	private JPanel myPanel;
	private JComboBox myCBox;
	private Conference myConference;
	private List<Conference> myConferences;
	
	/**
	 * Creates a Choose a Conference Panel.
	 */
    public ChooseConferencePanel() {
    	myPanel = new JPanel(getBorderLayout());
            
        //Display the window.    
        setUpContents(); 
        myConference = myConferences.get(0);
        myPanel.setOpaque(true);
    }
    
    public JPanel getPanel() {
    	return myPanel;
    }
    
    public Conference getConference(){
    	return myConference;
    }
    
    /**
     * Sets up the border layout.
     * @return BorderLayout object.
     */
    private static BorderLayout getBorderLayout() {
    	BorderLayout borderLayout = new BorderLayout();
    	borderLayout.setHgap(BORDER_SPACE);
    	return borderLayout;
    }
    
    /**
     * Creates and sets up components of JPanel.
     */
    private void setUpContents() {    	
        //Create JLabel "Please select Conference: "
        JLabel selectLabel = new JLabel("Please Select Conference: ");
        myConferences = getConferences();
 
        //Create the combo box
        myCBox = new JComboBox(getConferenceStrings());
        myCBox.addActionListener(
        	new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					myConference = myConferences.get(myCBox.getSelectedIndex());
				}	
        	}
        );
        myCBox.setSelectedIndex(0);
        
        //Create Next Button
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(
        		new ActionListener(){
			public void actionPerformed(ActionEvent theEvent) {
				setChanged();
				notifyObservers(State.CHOOSE_CONFERENCE);
				System.out.println("Selection is: " + myConferences.get(myCBox.getSelectedIndex()).getTitle());
			}
        });
       
        //Lay out.
        myPanel.add(selectLabel, BorderLayout.WEST);
        myPanel.add(myCBox, BorderLayout.CENTER);
        myPanel.add(nextButton, BorderLayout.EAST);
        myPanel.setBorder(BorderFactory.createEmptyBorder(BORDER_SPACE,BORDER_SPACE,BORDER_SPACE,BORDER_SPACE));
    }
    
    /**
     * Gets the names of the conferences.
     * @return The names of the conferences as a String array.
     */
    private String[] getConferenceStrings() {
    	String[] conferenceStrings = new String[myConferences.size()];
    	for (int i = 0; i < myConferences.size(); i++) {
    		conferenceStrings[i] = myConferences.get(i).getTitle();
    	}
    	return conferenceStrings;
    }
    
    private List<Conference> getConferences() {
    	return ConsumeXMLData.extractConferences("conferences.xml", Boolean.TRUE);
    }
    
	public String getTitle() {
		return TITLE;
	}
}
